package com.hdcx.Minfresh.adapter;


import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.TextAppearanceSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hdcx.Minfresh.R;
import com.hdcx.Minfresh.activity.OrderDetialReActivity;
import com.hdcx.Minfresh.activity.OrderUploadActivity;
import com.hdcx.Minfresh.bean.OrderListWrapper;
import com.hdcx.Minfresh.util.AppUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/9.
 */

public class MineOrderListAdapter extends RecyclerView.Adapter<MineOrderListAdapter.Holder> {
    FragmentActivity mActivity;
    OrderListWrapper data;
    List<OrderListWrapper.OrderListBean.GLIST> listData = new ArrayList<>();


    public MineOrderListAdapter(FragmentActivity mActivity) {
        this.mActivity = mActivity;
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        LayoutInflater layoutInflater = LayoutInflater.from(mActivity);
        view = layoutInflater.inflate(R.layout.item_fragment_detail_more, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(Holder holder, int position) {
        final OrderListWrapper.OrderListBean.GLIST itemtemp = listData.get(position);
        AppUtil.showPic(itemtemp.getGoods_img(), holder.img_main);
        holder.tv_name.setText(itemtemp.getGoods_title());
        holder.tv_intro.setText(itemtemp.getGoods_short_title());
        holder.tv_intro2.setText("规格：" + itemtemp.getGoods_etalon() + itemtemp.getGoods_units());
        holder.tv_units.setText(itemtemp.getOrder_quantity() + itemtemp.getGoods_units());
        holder.tv_money.setText("￥" + itemtemp.getGoods_price() + itemtemp.getGoods_units());
        holder.layout_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mActivity, OrderDetialReActivity.class);
                Log.e("MineOrderListAdapter", "MineOrderListAdapter--onClick--Father_id:"+itemtemp.getFather_id()+" Order_id:"+itemtemp.getOrder_id()
                +" id:"+data.getOrderlist().get(itemtemp.getFather_id()).getId());
                intent.putExtra("id", itemtemp.getOrder_id());
                mActivity.startActivity(intent);
            }
        });
        if (getItemViewType(position) == 3) {
            holder.layout_money.setVisibility(View.VISIBLE);
            holder.layout_no.setVisibility(View.VISIBLE);
            String temp = "￥" + data.getOrderlist().get(itemtemp.getFather_id()).getOrder_amount();
            SpannableString styledText = new SpannableString(temp);
            styledText.setSpan(new TextAppearanceSpan(mActivity, R.style.tv_red), 1, temp.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            styledText.setSpan(new TextAppearanceSpan(mActivity, R.style.tv_size_13), temp.length() - 3, temp.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            holder.tv_totle_money.setText(styledText, TextView.BufferType.SPANNABLE);
            if (data.getOrderlist().get(itemtemp.getFather_id()).getStatus().equals("1")) {
                holder.tv_go.setVisibility(View.VISIBLE);
                holder.tv_go.setText("立即支付");
                holder.tv_go.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mActivity, OrderUploadActivity.class);
                        intent.putExtra("id", data.getOrderlist().get(itemtemp.getFather_id()).getId());
                        mActivity.startActivity(intent);
                    }
                });
            } else {
                holder.tv_go.setVisibility(View.GONE);
            }
            holder.tv_no.setText("订单号：" + data.getOrderlist().get(itemtemp.getFather_id()).getOrder_sn());
            holder.tv_status.setText(data.getOrderlist().get(itemtemp.getFather_id()).getStatus_info());
        } else if (getItemViewType(position) == 1) {
            holder.layout_money.setVisibility(View.GONE);
            holder.tv_totle_money.setVisibility(View.GONE);
            holder.layout_no.setVisibility(View.VISIBLE);
            holder.tv_no.setText("订单号：" + data.getOrderlist().get(itemtemp.getFather_id()).getOrder_sn());
            holder.tv_status.setText(data.getOrderlist().get(itemtemp.getFather_id()).getStatus_info());
        } else if (getItemViewType(position) == 2) {
            String temp = "￥" + data.getOrderlist().get(itemtemp.getFather_id()).getOrder_amount();
            SpannableString styledText = new SpannableString(temp);
            styledText.setSpan(new TextAppearanceSpan(mActivity, R.style.tv_red), 1, temp.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            styledText.setSpan(new TextAppearanceSpan(mActivity, R.style.tv_size_13), temp.length() - 3, temp.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            holder.tv_totle_money.setText(styledText, TextView.BufferType.SPANNABLE);
            holder.layout_money.setVisibility(View.VISIBLE);
        } else {
            holder.layout_money.setVisibility(View.GONE);
            holder.layout_no.setVisibility(View.GONE);
        }

    }

    @Override
    public int getItemCount() {
        if (listData == null) {
            return 0;
        } else {
            return listData.size();
        }
    }

    @Override
    public int getItemViewType(int position) {
        return listData.get(position).getTypeView();

    }

    public void setData(OrderListWrapper response) {
        listData.clear();
        data = null;
        if (response.getOrderlist() == null) {
            return;
        }
        data = response;
        for (int i = 0; i < data.getOrderlist().size(); i++) {
            for (int j = 0; j < data.getOrderlist().get(i).getGlist().size(); j++) {
                if (j == 0) {
                    if (j + 1 == data.getOrderlist().get(i).getGlist().size()) {
                        Log.e("MineOrderListAdapter", "MineOrderListAdapter--setData--Father_id1111   "+i);
                        data.getOrderlist().get(i).getGlist().get(j).setFather_id(i);
                        data.getOrderlist().get(i).getGlist().get(j).setTypeView(3);
                        //有头和尾
                    } else {
                        data.getOrderlist().get(i).getGlist().get(j).setFather_id(i);
                        data.getOrderlist().get(i).getGlist().get(j).setTypeView(1);//有头
                    }
                } else if (j + 1 == data.getOrderlist().get(i).getGlist().size()) {
                    Log.e("MineOrderListAdapter", "MineOrderListAdapter--setData--Father_id2222   "+i);
                    data.getOrderlist().get(i).getGlist().get(j).setFather_id(i);
                    data.getOrderlist().get(i).getGlist().get(j).setTypeView(2);//有尾
                } else {
                    Log.e("MineOrderListAdapter", "MineOrderListAdapter--setData--Father_id3333   "+i);
                    data.getOrderlist().get(i).getGlist().get(j).setFather_id(i);
                    data.getOrderlist().get(i).getGlist().get(j).setTypeView(0);//中间
                }
                listData.add(data.getOrderlist().get(i).getGlist().get(j));
            }
        }
        notifyDataSetChanged();
    }

    public class Holder extends RecyclerView.ViewHolder {
        public LinearLayout layout_no, layout_all;
        public View layout_money;
        public TextView tv_totle_money, tv_intro, tv_intro2, tv_name, tv_units, tv_no, tv_money, tv_status, tv_go, textViewHeJi;
        public ImageView img_main;

        public Holder(View itemView) {
            super(itemView);
            layout_no = (LinearLayout) itemView.findViewById(R.id.layout_no);
            layout_money = itemView.findViewById(R.id.layout_money);
            layout_all = (LinearLayout) itemView.findViewById(R.id.layout_all);
            tv_totle_money = (TextView) itemView.findViewById(R.id.tv_totle_money);
            tv_money = (TextView) itemView.findViewById(R.id.tv_money);
            tv_intro = (TextView) itemView.findViewById(R.id.tv_intro);
            tv_intro2 = (TextView) itemView.findViewById(R.id.tv_intro_2);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            tv_units = (TextView) itemView.findViewById(R.id.tv_units);
            tv_no = (TextView) itemView.findViewById(R.id.tv_no);
            tv_status = (TextView) itemView.findViewById(R.id.tv_status);
            tv_go = (TextView) itemView.findViewById(R.id.tv_go);
            img_main = (ImageView) itemView.findViewById(R.id.img_main);
            textViewHeJi = (TextView) itemView.findViewById(R.id.textViewHeJi);
        }
    }
}
